package com.weedsdream.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput

/**一个占据全部可用空间的半透明灰块
 * @param color 背景色
 * @param alpha 背景透明度
 * @param content 浮在其上，不受背景影响的附加UI
 * @param onclick 取消了点击效果的背景点击事件。如果为null则不拦截点击*/
@Composable
fun Space(
    color: Color = Color.Gray,
    alpha: Float = 0.8f,
    content: @Composable (BoxScope.() -> Unit)? = null,
    onclick: (() -> Unit)? = null
) {
    Box(Modifier.fillMaxSize()) {
        var mod = Modifier
            .fillMaxSize()
            .alpha(alpha)
            .background(color)
        if (onclick != null) mod = mod.pointerInput(Unit) {
            detectTapGestures {
                onclick()
            }
        }
        Spacer(mod)
        if (content != null) content()
    }
}